<?php 
// 建立数据库连接 
include_once("./functions_for_filter.php");
$db_name = "filter_words";
$dbcnx = connect_db($db_name);
$tab_name = "level1_words_s";
// 获取当前页数 
if( isset($_GET['page']) ){ 
	$page = intval( $_GET['page'] ); 
} 
else{
	$page = 1; 
} 
// 每页数量 
$page_size = 20; 
// 获取总数据量，可以考虑用mysql_num_rows($result) 实现
$sql = "select count(*) as amount from $tab_name;"; 

$result = mysql_query($sql); 
$row = mysql_fetch_row($result); 
//echo $row[0] . $row[1] . "\n";
//$amount = $row["amount"];
$amount = $row[0];

// 记算总共有多少页 
if( $amount ){ 
	if( $amount < $page_size ){ $page_count = 1; } //如果总数据量小于$PageSize，那么只有一页 
	if( $amount % $page_size ){ //取总数据量除以每页数的余数 
		$page_count = (int)($amount / $page_size) + 1; //如果有余数，则页数等于总数据量除以每页数的结果取整再加一 
	}else{ 
		$page_count = $amount / $page_size; //如果没有余数，则页数等于总数据量除以每页数的结果 
	} 
} 
else{ 
	$page_count = 0; 
} 

// 翻页链接 
$page_string = ""; 
if( $page == 1 ){
	$page_string .= "第一页|上一页|"; 
} 
else{ 
	$page_string .= "<a href=\"". $_SERVER["PHP_SELF"] ."?page=1\">第一页</a>|<a href=\"". $_SERVER["PHP_SELF"] ."?page=". ($page-1) ."\">上一页</a>|";
}

if( ($page == $page_count) || ($page_count == 0) ){ 
	$page_string .= "下一页|尾页"; 
} 
else{ 
	$page_string .= "<a href=\"". $_SERVER["PHP_SELF"] ."?page=" . ($page+1) . "\">下一页</a>|<a href=\"". $_SERVER["PHP_SELF"] ."?page=" . $page_count . "\">尾页</a>"; 
} 

// 获取数据，以二维数组格式返回结果 
if( $amount ){
	$sql = "select * from $tab_name limit ". ($page-1)*$page_size . ", $page_size"; 
	$result = mysql_query($sql); 

	while ( $row = mysql_fetch_array($result, MYSQL_BOTH) ) {
		echo $row["word"] . "\n";
	}
	echo $page_string;
}else{ 
	$rowset = array(); 
} 
// 没有包含显示结果的代码，那不在讨论范围，只要用foreach就可以很简单的用得到的二维数组来显示结果
mysql_close ($dbcnx);
?> 

